package objectRank;

/**
 *
 * @author Reza Amini
 */
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

public class GraphTester {

    public static LinkedHashMap<Node, Double> sortHashMapByValues(HashMap<Node, Double> passedMap) {
        List<Node> mapKeys = new ArrayList(passedMap.keySet());
        List<Double> mapValues = new ArrayList(passedMap.values());
        Collections.sort(mapValues);
        Collections.reverse(mapValues);
        LinkedHashMap<Node, Double> sortedMap = new LinkedHashMap<Node, Double>();
        Iterator<Double> valueItr = mapValues.iterator();
        while (valueItr.hasNext()) {
            Double val = valueItr.next();
            Iterator<Node> keyItr = mapKeys.iterator();
            while (keyItr.hasNext()) {
                Node key = keyItr.next();
                Double comp = passedMap.get(key);
                if (comp == val){
                    sortedMap.put(key, val);
                    passedMap.remove(key);
                    mapKeys.remove(key);
                    break;
                }
            }
        }
        return sortedMap;
    }

    public static void main(String args[]) throws ClassNotFoundException{
        
        File file1 = new File("C:\\Users\\Reza\\Documents\\NetBeansProjects\\MyGroups"
                + "\\src\\java\\objectRank\\test.bin");
        //ObjectOutputStream objectOut = null;
        ObjectInputStream objectIn = null;
        QueryGraph graph = null;
        try{
            objectIn = new ObjectInputStream(new BufferedInputStream(
                    new FileInputStream(file1)));
            graph = (QueryGraph)objectIn.readObject();
                        
            Iterator<Integer> groupKeyIter = graph.identifyerToGroup.keySet().iterator();
            System.out.println("---------- Groups ----------");
            while(groupKeyIter.hasNext()) { // Iterate over the keys of groups
                int a = groupKeyIter.next();
                System.out.println(a + ": " + graph.identifyerToGroup(a));
            }
            Iterator<Integer> UserKeyIter = graph.identifyerToUser.keySet().iterator();
            System.out.println("----------  Users  ----------");
            while(UserKeyIter.hasNext()) { // Iterate over the keys of groups
                int a = UserKeyIter.next();
                System.out.println(a + ": " + graph.identifyerToUser(a));
                Iterator<Integer> herGroups = graph.identifyerToUser(a).inLinksFromGroups.keySet().iterator();
                System.out.println("\tmember of groups: ");
                while(herGroups.hasNext()){
                    System.out.println("%%%%" + graph.identifyerToGroup(herGroups.next()));
                    System.out.println();
                }
            }
//            Iterator<Integer> DocumentKeyIter = graph.identifyerToDocument.keySet().iterator();
//            System.out.println("--------- Documents ---------");
//            while(DocumentKeyIter.hasNext()) {
//                int a = DocumentKeyIter.next();
//                System.out.println(a + ": " + graph.identifyerToDocument(a));
//            }
            Iterator<Integer> queryKeyIter = graph.identifyerToQuery.keySet().iterator();
            System.out.println("---------- Queries ----------");
            while(queryKeyIter.hasNext()) {
                int a = queryKeyIter.next();
                System.out.println(a + ": " + graph.identifyerToQuery(a));
                graph.calculateRootSet(graph.identifyerToQuery(a));
                graph.calculateBaseSet();
                HashMap<Node, Double> ranks = graph.calculateObjectRank();
                ranks = graph.sortHashMapByValues(ranks);
                Iterator<Node> itr = ranks.keySet().iterator();
                double rank = 0;
                Node b = null;
                while(itr.hasNext()){
                    b = itr.next();
                    rank = ranks.get(b);
                    System.out.println("Node " + "\"" + b + "\":" + rank);
                }
                System.out.println("=========================");
            }


            objectIn.close(); // Close the input stream
            //file1.delete(); // Delete the file to write the updated QueryObject again
            //file1.createNewFile();
            //objectOut = new ObjectOutputStream(new BufferedOutputStream(
            //        new FileOutputStream(file1)));
            //objectOut.writeObject(graph);
            //objectOut.close(); // Close the output stream

        } catch(ClassNotFoundException e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }catch(IOException e){
            System.err.println("Error: " + e);
        }
    }
}
